var e = require("../../requests/request.js"), t = require("../../requests/newapi.js").decryptUserInfo, a = getApp();

Page({
    data: {
        userName: "",
        userAvatar: "",
        isLogin: !1,
        length: 0
    },
    getUserInfo: function() {
        var e = wx.getStorageSync("UserName"), t = wx.getStorageSync("UserAvatar"), n = wx.getStorageSync("UserToken");
        e && t && n ? (a.globalData.hasLogin = !0, this.setData({
            userName: e,
            userAvatar: t,
            isLogin: !0
        })) : this.setData({
            userName: "立即登录",
            userAvatar: "",
            isLogin: !1
        });
    },
    getUnionId: function(e) {
        var a = e.detail, n = a.iv, o = a.encryptedData;
        if (!n || !o) return Promise.reject({
            code: 0,
            msg: "用户未授权"
        });
        var s = wx.getStorageSync("session_key");
        return new Promise(function(e, a) {
            t({
                op: "encrypteddata",
                iv: n,
                encrypteddatastr: o,
                sessionkey: s
            }).then(function(t) {
                return e(t.unionId);
            }).catch(function() {
                return a({
                    code: 1,
                    msg: "获取登录信息错误"
                });
            });
        });
    },
    getAuthorize: function(t) {
        var n = this;
        this.data.isLogin || (wx.showLoading({
            mask: !0
        }), this.getUnionId(t).then(function(o) {
            e.login(o, function(e) {
                2 == e.Status ? (wx.setStorageSync("UserToken", e.Data.UserToken), wx.setStorageSync("UserId", e.Data.UserId), 
                wx.setStorageSync("UserName", e.Data.UserName), wx.setStorageSync("UserAvatar", e.Data.UserAvatar), 
                wx.setStorageSync("UserStatus", ""), n.setData({
                    userName: e.Data.UserName,
                    userAvatar: e.Data.UserAvatar,
                    isLogin: !0
                }), a.globalData.hasLogin = !0) : 21 == e.UserStatus ? (wx.setStorageSync("UserStatus", 21), 
                wx.setStorageSync("useractivatetoken", e.Data.useractivatetoken), wx.navigateTo({
                    url: "../login/login"
                })) : (wx.setStorageSync("UserName", t.detail.userInfo.nickName), wx.setStorageSync("UserAvatar", t.detail.userInfo.avatarUrl), 
                wx.setStorageSync("UserStatus", ""), wx.navigateTo({
                    url: "../login/login"
                })), wx.hideLoading();
            });
        }).catch(function(e) {
            var t = e.code, a = e.msg;
            wx.hideLoading(), wx.showToast({
                title: a,
                icon: "none"
            }), 0 === t && n.setData({
                userName: a,
                userAvatar: ""
            });
        }));
    },
    signOut: function() {
        this.setData({
            userName: "立即登录",
            userAvatar: "",
            isLogin: !1
        }), wx.setStorageSync("UserName", ""), wx.setStorageSync("UserAvatar", ""), wx.setStorageSync("UserToken", ""), 
        wx.setStorageSync("UserStatus", ""), wx.setStorageSync("UserId", ""), a.globalData.hasLogin = !1;
    },
    abouts: function() {
        wx.navigateTo({
            url: "../abouts/abouts"
        });
    },
    GetVoteData: function() {
        var t = this, a = wx.getStorageSync("UserToken"), n = wx.getStorageSync("UserId"), o = wx.getStorageSync("opid"), s = new Date(), r = parseInt(s.getTime() / 1e3), i = this.data.pindex > 1 ? wx.getStorageSync("LastUpdatetime") : r;
        e.getVoteList("vote.topiclist", 1, 10, 107, a, n, o, i, function(e) {
            t.setData({
                length: e.Data.List.length
            });
        }, function(e) {}, function(e) {});
    },
    onLoad: function(e) {
        this.getUserInfo();
    },
    onReady: function() {},
    onShow: function() {
        this.getUserInfo(), wx.getStorageSync("UserToken") && this.GetVoteData();
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    adduserfeedback: function() {
        wx.login({
            success: function(t) {
                t.code && e.getOpenId(t.code, function(e) {
                    wx.navigateTo({
                        url: "../adduserback/adduserback?OpenId=" + e.openid
                    });
                });
            }
        });
    },
    collectionCar: function() {
        wx.navigateTo({
            url: "../collect/collect"
        });
    },
    selectCar: function() {
        wx.getStorageSync("UserToken") && this.data.length ? wx.navigateTo({
            url: "../voteList/voteList"
        }) : wx.navigateTo({
            url: "../addhelp/addhelp"
        });
    },
    toEnquireRecord: function() {
        wx.navigateTo({
            url: "../enquireRecord/enquireRecord"
        });
    }
});